# compute cluster coefficient

ograph = ARGV[0]
edgePattern=/(\S+)\s+\{(\d+)\,(\d+)\,(\d+|-\d+)\,(\d+|-\d+)\,(\w).*?\}/  

$reads = {}

File.new(ograph,'r').each do |line|
  if line=~ /^(\S+)\s+\S+/
    node = $1
    $reads[node] = {} unless $reads.key?(node)
  end
  line.scan(edgePattern) do |refName, match,score,l_ex,r_ex,dir|
    $reads[node][refName] = 1
   
    if dir == 'f'
      l_ex = l_ex.to_i
      r_ex = r_ex.to_i
      dir = 1
    else
      l_ex, r_ex = r_ex.to_i, l_ex.to_i
      dir = -1
    end
  end
end

$reads.each_key do |r|
  k = $reads[r].size
  n = 0
  $reads[r].each_key do |a|
    $reads[r].each_key do |b|
      next if a == b
      if ($reads.key?(a) and $reads[a].key?(b) ) or ($reads.key?(b) and $reads[b].key?(a))
        n += 1
      end
    end
  end
  
  if k> 1
    cc = 1.0 * n / (k*(k-1))
    
    puts "#{k}\t#{cc}"
  end

end
    
